package com.hh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hh.entity.StatisticsDaily;
import com.hh.mapper.StatisticsDailyMapper;
import com.hh.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.service.client.UcenterClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author huhuan
 * @since 2022-02-23
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private StatisticsDailyMapper mapper;
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void getRegisterCount(String date) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", date);
        mapper.delete(wrapper);

        int countOfDay = ucenterClient.getRegisterCountOfDay(date);
        StatisticsDaily daily = new StatisticsDaily();
        daily.setDateCalculated(date);
        daily.setRegisterNum(countOfDay);
        daily.setLoginNum(new Random().nextInt(100));
        daily.setVideoViewNum(new Random().nextInt(100));
        daily.setCourseNum(new Random().nextInt(100));
        daily.setGmtCreate(new Date());
        daily.setGmtModified(new Date());

        mapper.insert(daily);
    }

    @Override
    public Map getDailtBetwenBeginAndEnd(String begin, String end, String type) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();

        wrapper.between("date_calculated", begin, end);
        List<StatisticsDaily> dailyList = mapper.selectList(wrapper);

        ArrayList<Integer> dataList = new ArrayList<>();
        ArrayList<String> dateList = new ArrayList<>();

        for (StatisticsDaily daily : dailyList) {
            dateList.add(daily.getDateCalculated());

            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
            }
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("dataList", dataList);
        map.put("dateList", dateList);
        return map;
    }
}
